<template>
    <div>
        <h1>房号编辑</h1>
        <table class="table">
            <tbody>
                <tr>
                    <td>房间号</td>
                    <td><input v-model="obj.hname"></td>
                </tr>
                <tr>
                    <td>房间类型</td>
                    <td><select v-model="obj.tid">
                        <option value="0">请选择</option>
                        <option v-for="e in Tid" :value="e.tid">{{e.tname}}</option>
                    </select></td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td><select v-model="obj.state">
                        <option value="-1">请选择</option>
                        <option value="1">已入住</option>
                        <option value="2">空闲</option>
                        <option value="3">打扫中</option>
                    </select></td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td><input type="file" @change="Upload">
                    <img :src="'https://localhost:7112'+imgPath" style="width: 100px;height: 100px;"></td>
                </tr>
                <tr>
                    <td>价格</td>
                    <td><input v-model="obj.price"></td>
                </tr>
               
                <tr>
                    <td></td>
                    <td><input type="button" value="修改" @click="Updae"></td>
                    <a href="/">显示列表</a>
                </tr>
            </tbody>
        </table>
    </div>
</template>
<script setup lang="ts">
import {ref} from 'vue';
import { onMounted } from 'vue';
import { useRoute,useRouter } from 'vue-router';
import axios from 'axios';
import moment from 'moment';
const route=useRoute();
onMounted(() => {
  Bang()
  fan()
})
const fan=()=>{
    var val=route.query.id;
    axios.get("https://localhost:7112/api/All/Fan",{params:{id:val}}).then(res=>{
        obj.value=res.data
    })
}


const obj=ref({
"hid": 0,
  "tid": -1,
  "hname": " ",
  "img": "",
  "state": -1,
  "del": true,
  "price": ""
})
const Tid=ref([{
    "tid": 1,
    "tname": "标准间",
   
}])
const Bang=()=>{
    axios.get("https://localhost:7112/api/All/Bang",{params:Tid.value}).then(res=>{
        Tid.value=res.data;
    })
}
const Router=useRouter();
const Updae=()=>{
    obj.value.img=imgPath.value;
    axios.put("https://localhost:7112/api/All/Update",obj.value).then(res=>{
        if(res.data==0)
    {
        alert('编辑失败');
        return;
    }
    else{
        alert('编辑成功');
        Router.push({path:"/"});
    }
    })
}
//取文件封装包 把文件保存到包里
const imgPath=ref("");
const Upload=(e:any)=>{
    var f=e.target.files[0];
    var fd=new FormData();
    fd.append("file",f);
    axios.post("https://localhost:7112/api/Up/Upload",fd).then(res=>{
        imgPath.value=res.data;
    })
}

</script>

<style scoped>

</style>